#include <iostream>
#include <string>
#include <boost/format.hpp>
#include <sys/time.h>

using namespace std;
using boost::format;

namespace seapilot
{
    pid_t gv_pid = getpid();
    string GetTimeStr()
    {
        struct timeval tv;
        struct tm      tm_val;

        if (gettimeofday(&tv, NULL) < 0)
            tv.tv_sec = tv.tv_usec = 0;

        if (localtime_r(&tv.tv_sec, &tm_val))
            return str(format("%d-%02d-%02d %02d:%02d:%02d.%06d(%d)") 
                               % (tm_val.tm_year + 1900)
                               % (tm_val.tm_mon  + 1)
                               % tm_val.tm_mday
                               % tm_val.tm_hour
                               % tm_val.tm_min
                               % tm_val.tm_sec
                               % tv.tv_usec
                               % gv_pid);
        else
            return " ";
    }

    class Stdout
    {
    public:
        template <typename T>
        ostream& operator << (const T &value) {
            cout << GetTimeStr() << " " << value;
            return cout;
        }
    
        // this is specific for endl, since it is not a variable
        ostream& operator << (basic_ostream<char, char_traits<char> >& (*_Pfn)(basic_ostream<char, char_traits<char> >&) )
        {
            cout << endl;
            return cout;
        }
    };

}

seapilot::Stdout sp_cout;

int main()
{
    int i=5;
    double d=1.2;
    string str="Hello";

    sp_cout << endl;
    sp_cout << i << endl;
    sp_cout << d << endl;
    sp_cout << str << endl;
    sp_cout << i << "  " << d << "   " << str << endl;

    return 0;
}
